<template>
  <div class="box section clearfix">
    <el-form :inline="true" style="text-align: left" :model="query">
      <el-row>
        <el-form-item label="关键词：">
          <el-input
            placeholder="产品名称、数据表"
            v-model="query.keyWord"
          ></el-input>
        </el-form-item>
        <el-form-item label="产品状态：">
          <el-select v-model="query.productStatus" placeholder="请选择">
            <el-option
              v-for="item in accountData"
              :key="item.itemValue"
              :label="item.itemText"
              :value="item.itemValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="danger" @click="clearFun" v-no-more-click
            >清空</el-button
          >
          <el-button type="primary" @click="queryFun" v-no-more-click
            >搜索</el-button
          >
        </el-form-item>
        <el-col :span="24">
          <el-form-item>
            <el-button @click="addProduct" v-if="showAddBtn" v-no-more-click
              >添加产品模型</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column
        prop="id"
        label="产品ID"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="productName"
        label="产品名称"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="productBrief"
        label="产品介绍"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="dataForm"
        label="数据表"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="tableCode"
        label="录入表单代码"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="modelNumber"
        label="模型交单数量"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="approveFlowId"
        label="审批流"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="productStatus"
        label="产品状态"
        align="center"
        :formatter="postStatusFormat"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            v-if="showEditBtn"
            @click="editorProduct(scope.row)"
            style="color: #ffa42f"
            v-no-more-click
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="showEditBtn"
            @click="iconFun(scope.row)"
            style="color: #ffa42f"
            v-no-more-click
            >图标</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="showEditBtn"
            @click="promotionFun(scope.row)"
            style="color: #ffa42f"
            v-no-more-click
            >推广图</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="pageButton(scope.row)"
            style="color: #ffa42f"
            v-no-more-click
            >着陆页</el-button
          >
          <!-- <el-dialog
            title="产品推广图"
            :visible.sync="test"
            width="30%"
            :modal="true"
            v-if="test"
            :append-to-body="true"
            top="1vh"
          > -->
          <!-- <getLandingPages
          
           
              @promotionClose="getLandingPagess"
              :promotionId="promotionId"
              :getLandingPageImage="getLandingPageImage"
              :AccordingData="AccordingData"
            ></getLandingPages> -->
          <!-- </el-dialog> -->

          <el-popconfirm
            title="确定删除此数据吗？"
            @onConfirm="deleteData(scope.$index, scope.row)"
            v-if="showDeteBtn"
          >
            <el-button
              slot="reference"
              type="text"
              size="small"
              v-no-more-click
              style="color: #fb575e"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--分页控件-->
    <div class="pagination">
      <span class="title">小计：共{{ this.total }}条数据</span>
      <el-pagination
        background
        layout="total,prev, pager, next, sizes"
        :total="total"
        :page-size="query.pagesize"
        :page-sizes="[10, 20, 30, 40]"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </div>
    <!-- 新增修改对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      width="900px"
      class="dialogBox"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <ProductDialog
        class="addProductStyle"
        :dialogVisible="dialogVisible"
        :productId="productId"
        v-on:dialogVisible="closeDialog($event, dialogVisible)"
        :getList="getList"
      ></ProductDialog>
    </el-dialog>

    <!-- 推广图 -->
    <el-dialog
      title="产品推广图"
      :visible.sync="dialogVisiblePromotion"
      width="30%"
      v-if="dialogVisiblePromotion"
    >
      <ProductPromotion
        @promotionClose="promotionCloseFun"
        :promotionId="promotionId"
        :promotionData="promotionData"
        :AccordingData="AccordingData"
      ></ProductPromotion>
    </el-dialog>
    <!-- 图标 -->
    <el-dialog
      title="产品图标"
      :visible.sync="dialogVisibleIcon"
      width="30%"
      v-if="dialogVisibleIcon"
    >
      <ProductIcon
        :iconId="iconId"
        @IconClose="IconCloseFun"
        :iconUrl="iconUrl"
      ></ProductIcon>
    </el-dialog>

    <el-dialog title="着陆页:" :visible.sync="pageSwitch" width="30%">
      <getLandingPages
        v-if="pageSwitch"
        @promotionClose="getLandingPagess"
        :promotionId="promotionId"
        :getLandingPageImage="getLandingPageImage"
        :AccordingData="AccordingData"
      ></getLandingPages>
    </el-dialog>
  </div>
</template>

<script>
import ProductDialog from "../../components/business/ProductDialog";
import ProductIcon from "../../components/productUpload/ProductIcon";
import ProductPromotion from "../../components/productUpload/ProductPromotion";
import getLandingPages from "../../components/productUpload/getLandingPage.vue";
import {
  getProductList,
  getAstatus,
  delProduct,
  listPushImgProduct,
  getLandingPage, //着陆页接口!
  getStatus, //显示下拉框接口!
} from "../../../src/assets/js/api";
import { checkPermiss } from "../../assets/js/utlis";
export default {
  name: "ProductModel",
  components: { ProductDialog, ProductIcon, ProductPromotion, getLandingPages },
  data() {
    return {
      AccordingData: [], //显示下拉框数据!
      productId: true,
      dialogVisible: false,
      title: "",
      total: 10,
      pages: 0,
      pageSwitch: false, //着陆页弹出层开关!
      getLandingPageImage: [], //着陆页数据列表!
      query: {
        pagesize: 10,
        pageNo: 1,
        keyWord: null,
        productStatus: null,
      },
      accountData: [],
      tableData: [],
      whetherData: [],
      golbalPermissions: null,
      dialogVisiblePromotion: false,
      dialogVisibleIcon: false,
      iconId: "",
      iconUrl: {
        name: "",
        url: "",
      },
      promotionData: [],
      promotionId: "",
      test: false,
    };
  },
  created() {
    this.golbalPermissions = localStorage.getItem("permissions").split(",");
    this.getList();
    this.getAstatus();
  },
  computed: {
    //添加产品按钮
    showAddBtn: function () {
      return checkPermiss("business:product:save", this.golbalPermissions);
    },
    //编辑产品按钮
    showEditBtn: function () {
      return checkPermiss("business:product:update", this.golbalPermissions);
    },
    //删除产品按钮
    showDeteBtn: function () {
      return checkPermiss("business:product:delete", this.golbalPermissions);
    },
  },

  methods: {
    getLandingPagess(data) {
      this.pageSwitch = data;
      this.getList(this.query);
    },
    //操作-着陆页按钮!
    handleClose() {
      this.pageSwitch = false;
    },
    pageButton(row) {
      //获取下拉框数据!
      getStatus().then((res) => {
        this.AccordingData = res.data;
      });
      //通过这条数据的ID获取推广图数据!
      this.promotionId = row.id;
      getLandingPage(this.promotionId).then((res) => {
        this.getLandingPageImage = res.data;

        this.pageSwitch = true;

        //this.getLandingPageImage是着陆页数据列表!
      });
    },
    // 获取表格数据
    getList(obj) {
      getProductList(obj).then((res) => {
        this.tableData = res.data.records;
        this.query.pageNo = res.data.current;
        this.total = res.data.total;
        this.pages = res.data.pages;
        this.tableData.map((item) => {
          if (!item.approveFlowId) {
            item.approveFlowId = "否";
          } else {
            item.approveFlowId = "是";
          }
        });
      });
    },
    // 查询字典产品状态
    getAstatus() {
      getAstatus("products_state").then((res) => {
        let arr = res.data;
        let arr2 = arr.map(function (arr) {
          arr.id = parseInt(arr.itemValue);
          return arr;
        });
        this.accountData = arr2;
      });
    },
    postStatusFormat(row) {
      return this.selectDictLabel(this.accountData, row.productStatus);
    },
    // 产品推广图
    promotionFun(row) {
      this.promotionId = row.id;
      //获取下拉框数据!
      getStatus().then((res) => {
        this.AccordingData = res.data;
      });
      //  通过这条数据的ID获取推广图数据!
      listPushImgProduct(row.id).then((res) => {
        this.dialogVisiblePromotion = true;
        if (res.code == 200) {
          //this.promotionData推广图数据集合!
          this.promotionData = res.data;
        } else {
          this.dialogVisiblePromotion = true;
        }
      });
    },
    // 产品图标
    iconFun(row) {
      this.dialogVisibleIcon = true;
      this.iconUrl.name = row.productShowName;
      this.iconUrl.url = row.productSaveName;
      this.iconId = row.id;
    },
    IconCloseFun(data) {
      this.dialogVisibleIcon = data;
      this.getList(this.query);
    },
    promotionCloseFun(data) {
      this.dialogVisiblePromotion = data;
      this.getList(this.query);
    },
    // 搜索功能
    queryFun() {
      this.query.pageNo = 1;
      this.getList(this.query);
    },
    // 清空按钮
    clearFun() {
      this.query.keyWord = null;
      this.query.pagesize = 10;
      this.query.productStatus = null;
      this.getList();
    },
    // 编辑按钮操作
    editorProduct(obj) {
      this.productId = obj.id;

      this.dialogVisible = true;
      this.title = "修改产品模型";
    },
    // 添加按钮操作
    addProduct() {
      this.dialogVisible = true;
      this.title = "添加产品模型";
    },
    // 删除操作
    deleteData(index, obj) {
      delProduct(obj.id).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: res.message,
            type: "success",
          });
          this.getList(this.query);
        } else {
          this.$message({
            message: res.message,
            type: "error",
          });
          this.getList(this.query);
        }
      });
    },
    closeDialog(msg, isNew) {
      this.dialogVisible = msg;

      this.productId = isNew;
    },
    //分页
    handleCurrentChange(cpage) {
      this.query.pageNo = cpage;
      this.getList(this.query);
    },
    handleSizeChange(psize) {
      this.query.pagesize = psize;
      this.getList(this.query);
    },
  },
  //监听删除数据如果是这一页的最后一条数据，返回上一页
  watch: {
    total: function () {
      if (
        this.total == (this.query.pageNo - 1) * this.query.pagesize &&
        this.totalotal != 0
      ) {
        this.query.pageNo -= 1;
        this.getList(this.query); //获取列表数据
      }
    },
  },
};
</script>

<style scoped type="less">
.layer-box {
  overflow: auto;
  height: auto;
  max-height: 500px;
}
.dialogBox {
  /* position: absolute; */
  top: -15%;
  left: 0;
  margin: 0 !important ;
}
::v-deep .el-form--inline .el-form-item__content {
  line-height: 42px;
  width: 210px;
}
::v-deep .el-table td {
  padding: 10px 0;
}
::v-deep .el-table th {
  padding: 10px 0;
}
::v-deep .el-input__inner {
  height: 32px;
}
::v-deep .el-input__icon {
  line-height: 32px;
}
::v-deep .btn .el-button {
  line-height: 0;
  width: 80px;
  height: 32px;
}
::v-deep .el-table th > .cell {
  font-size: 14px;
  font-weight: bold;
  color: #333;
}
::v-deep .el-table--striped .el-table__body tr.el-table__row--striped td {
  background: rgba(59, 157, 248, 0.1);
}
.box {
  position: relative;
  padding: 20px 20px 50px 20px;
  background: #fff;
}
::v-deep .el-pagination {
  position: absolute;
  right: 0;
  margin-top: 20px;
}
::v-deep .el-pagination__total {
  visibility: hidden;
}
.title {
  font-family: Microsoft YaHei;
  font-size: 12px;
  font-weight: 400;
  line-height: 21px;
  position: absolute;
  left: 0;
  margin-top: 15px;
  color: rgba(51, 51, 51, 1);
}
/* 产品模型弹出层添加滚动条!
 */
.addProductStyle {
  height: 800px;
  overflow: auto;
}
.addProductStyle::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 8px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 1px;
}
.addProductStyle::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 10px;
  background: #9e9e9e;
  box-shadow: inset 0 0 5px red(0, 122, 204);
}

.addProductStyle::-webkit-scrollbar-track {
  border-radius: 10px;
  background: #ededed;

  /*滚动条里面轨道*/
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
}
</style>
